<?php
/**
 * the base item controller manages the basic functionality that a content item needs to save
 *
 */
class DigCms_ItemController extends Zend_Controller_Action {
    public function init() {
        DigUi_Service_Template::useAdminTemplate();
        parent::init();
    }

    public function indexAction() {
        $pageModel = new DigCms_Model_Item();
        $this->view->pages = $pageModel->fetchAll();
    }

    public function createAction() {
        $rel = $this->getRequest()->getParam('rel');
        $ref = $this->getRequest()->getParam('ref');
        $parent = DigCms_Model_Item::getParentByReference($ref, $rel);
        $child = $parent->appendChild();
        $child->move($ref, $rel);
        $newName = strtolower($child->name . '_' . ($child->position + 1));
        $child->name = str_replace(' ', '_', trim($newName));
        $child->save();
        $this->_helper->json($child->toArray());
    }

    public function editAction() {
        if(! is_null($this->getRequest()->getParam('cancel'))) {
            return $this->_forward('index');
        }

        $id = $this->getRequest()->getParam('id');
        $item = DigCms_Model_Item::factory($id);
        $this->view->item = $item;
        $type = DigCms_Service_Item::getItemTypeDetails($item->namespace);
        if(!$type) {
            $type = DigCms_Service_Item::getDefaultType();
        }
        $this->view->type = $type;
    }

    public function renameAction() {
        $id = $this->getRequest()->getParam('id');
        $name = $this->getRequest()->getParam('name');

        // replace all underscores, spaces, and hyphens with underscores
        $name = strtolower($name);
        $name = str_replace('_', ' ', trim($name) );
        $name = str_replace('-', ' ', trim($name) );
        $name = Zend_Filter::filterStatic($name, 'StripTags');
        $name = str_replace(' ', '_', trim($name) );
        $item = DigCms_Model_Item::factory($id);
        $item->rename(trim($name));
        $this->_helper->json($item->toArray());
    }

    public function setPublishLevelAction() {
        $id = $this->getRequest()->getParam('id');
        $publish_level = $this->getRequest()->getParam('publish_level');
        $levels = DigCms_Service_Item::getPublishLevels();
        if(array_key_exists($publish_level, $levels)) {
            $item = DigCms_Model_Item::factory($id);
            $item->publish_level = $publish_level;
            $item->save();
            $response = array(
                    'item'	        => $item->toArray(),
                    'publish_level'	=> $levels[$publish_level]
            );
        } else {
            $response = false;
        }
        $this->_helper->json($response);
    }

    public function setItemTypeAction() {
        $id = $this->getRequest()->getParam('id');
        $type = $this->getRequest()->getParam('type');
        $item = DigCms_Model_Item::factory($id);
        $types = DigCms_Service_Item::getItemTypes();
        if(array_key_exists($type, $types)) {
            $currentType = $types[$type]['name'];
        } else {
            $currentType = null;
        }
        $item->namespace = $type;
        $item->save();
        $response = array(
            'item'  => $item->toArray(),
            'type'	=> $currentType
        );
        $this->_helper->json($response);
    }

    public function deleteAction() {
        $page = DigCms_Model_Item::factory($this->getRequest()->getParam('id'));
        $response = $page->delete();
        $this->_helper->json($response);
    }

    public function moveAction() {
        $id = $this->getRequest()->getParam('id');
        $referenceId = $this->getRequest()->getParam('ref');
        $rel = $this->getRequest()->getParam('rel');
        $item = DigCms_Model_Item::factory($id);
        $response = $item->move($referenceId, $rel);
        $this->_helper->json($response);
    }

    public function propertiesAction() {
        $item = DigCms_Model_Item::factory($this->getRequest()->getParam('id'));
        $this->view->properties = DigCms_Service_Item::properties($item);
    }

    public function jsonDetailsAction() {
        $id = $this->getRequest()->getParam('id', null);
        $this->_helper->json(DigCms_Model_Item::factory($id)->toArray());
    }

    public function treeDataAction() {
        $id = $this->getRequest()->getParam('id', 0);
        if($id == 0) {
            $id = $this->getRequest()->getParam('start', null);
        }

        if($id > 0) {
            $item = DigCms_Model_Item::factory($id);
            if(isset($item->type) && $item->type == 'dir' && !$item->isLoaded()) {
                $item->synchronize();
            }
            $children = $item->children();
        } else {
            $cmsItemModel = new DigCms_Model_Item();
            $children = $cmsItemModel->fetchAll(
                    $cmsItemModel->select(false)->where('parent_id = 0')
            );
        }
        if(count($children) > 0) {
            $defaultType = DigCms_Service_Item::getDefaultType();
            foreach ($children as $child) {
                $childData = array();
                $childData['data'] = $child->name;
                if($child->hasChildren()) {
                    $childData['state'] = 'closed';
                }
                $namespace = (!empty($child->namespace)) ? $child->namespace: $defaultType['namespace'];
                if(! isset($child->show_on_tree) || $child->show_on_tree == true) {
                    $childData['attributes'] = array(
                        'id' => $child->id,
                        'content_type'  => $namespace,
                        'class' => $namespace
                        );
                    $json[] = $childData;
                }
            }
        }
        $this->_helper->json($json);
    }
}
?>